package com.zt.rongda.stock.service;

import java.text.SimpleDateFormat;
import com.zt.rongda.stock.inter.StockTransferInMapper;
import com.zt.rongda.stock.model.Stock;
import com.zt.rongda.stock.model.StockTransferIn;
import com.zt.system.model.BusinessException;
import com.zt.servlet.ReqBean;
import com.zt.system.model.SqlBuilder;
import com.zt.system.service.BaseService;
import com.zt.util.DateUtil;
import com.zt.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class StockTransferInService extends BaseService {

    @Autowired
    private StockTransferInMapper stockTransferInMapper;
    @Autowired
    private StockService stockService;

    /**
    * 查询
    *
    * @param reqBean
    * @return
    */
    public JSONObject getTable(ReqBean reqBean) {
        SqlBuilder sqlBuilder = new SqlBuilder("");
        sqlBuilder.appendSql("SELECT a.LIST_ID,a.STOCK_ID,a.STOCK_NAME,a.TYPE_ID,a.TYPE_NAME,a.SPEC,a.BRAND,a.UNIT,IF(a.C_TYPE=0,a.NUM,-a.NUM) AS SHOW_NUM,a.NUM,a.PRICE,a.TOTAL_PRICE,a.STOCK_DATE,a.LOCATION,a.SUPPLIER_ID,a.SUPPLIER_NAME,a.C_SETTLEMENT,a.PURPOSE,a.CONTENT,a.USER_ID,a.USER_NAME,a.PURCHASE_LIST_ID,a.C_TYPE,a.OPERATOR,a.MAKE_TIME,a.MODIFY_TIME,a.REMARK FROM STOCK_TRANSFER_IN a where 1=1 ");

        sqlBuilder.addWhereLike2("STOCK_NAME", reqBean.getInfo("stockName"));
        sqlBuilder.addWhereLike2("TYPE_ID", reqBean.getInfo("typeId"));

        // 时间间隔
        String endDate = reqBean.getInfo("makeTimeEnd");
        if(endDate != null && endDate.length()>=10)
        {
            Date tmp_date =  DateUtil.parse(endDate);

            SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
            endDate = df.format(new Date(tmp_date.getTime() + 1 * 24 * 60 * 60 * 1000)); // 结束日期加一天
        }
        sqlBuilder.addTimeWhere("MAKE_TIME", reqBean.getInfo("makeTimeStart"), ">=");
        sqlBuilder.addTimeWhere("MAKE_TIME", endDate, "<");
        //

        return queryTable(sqlBuilder, reqBean.getPageSize(), reqBean.getPageIndex());
    }

    /**
    * 通过id查询
    *
    * @param listId
    * @return
    */
    public StockTransferIn getById(String listId) {
        return stockTransferInMapper.selectByPrimaryKey(listId);
    }

    /**
    * 新增
    *
    * @param stockTransferIn
    */
    public void add(StockTransferIn stockTransferIn) {
        stockTransferIn.setListId(stringId());// 手动设置Id

        stockTransferIn.setMakeTime(new Date());
        stockTransferIn.setModifyTime(stockTransferIn.getMakeTime());
        stockTransferInMapper.insert(stockTransferIn);

        Stock stock = getStock(stockTransferIn);

        String isBacked = stockTransferIn.getType();

        if("0".equals(isBacked)){  //入库
            if(stockService.isExisted(stock)){
                stockService.increase(stock);
            }else{
                stockService.add(stock);
            }
        }else if("1".equals(isBacked)){    // 退货情况
            if(stockService.isExisted(stock) && stockService.isEnough(stock)){
                stockService.decrease(stock);
            }
        }

    }

    /**
    * 修改
    *
    * @param stockTransferIn
    */
    public void update(StockTransferIn stockTransferIn) {

        //库存更新
        Stock stock = getStock(stockTransferIn);

        StockTransferIn oldStockTransferIn = stockTransferInMapper.selectByPrimaryKey(stockTransferIn.getListId());
        Stock oldStock = getStock(oldStockTransferIn);

        String isBacked = stockTransferIn.getType();

        if("0".equals(isBacked)){
            if(stockService.isExisted(oldStock) && stockService.isEnough(oldStock)){
                stockService.decrease(oldStock);
            }

            stockService.increase(stock);
        }else if("1".equals(isBacked)){   // 退货情况
            stockService.increase(oldStock);

            if(stockService.isExisted(oldStock) && stockService.isEnough(oldStock)){
                stockService.decrease(stock);
            }
        }

        //更新入库记录
        String listId = stockTransferIn.getListId();
        if (StringUtil.isEmpty(listId)) {
            throw new BusinessException("获取主键失败");
        }

        stockTransferIn.setModifyTime(new Date());
        stockTransferInMapper.updateByPrimaryKeySelective(stockTransferIn);

    }

    /**
    * 删除
    *
    * @param stockTransferIn
    */
    public void delete(StockTransferIn stockTransferIn) {

        String listId = stockTransferIn.getListId();
        if (StringUtil.isEmpty(listId)) {
            throw new BusinessException("获取主键失败");
        }
        stockTransferInMapper.deleteByPrimaryKey(listId);

        Stock stock = getStock(stockTransferIn);

        String isBacked = stockTransferIn.getType();

        if("0".equals(isBacked)){  //入库
            if(stockService.isExisted(stock) && stockService.isEnough(stock)){
                stockService.decrease(stock);
            }
        }else if("1".equals(isBacked)){    // 退货情况
            if(stockService.isExisted(stock)){
                stockService.increase(stock);
            }
        }

    }

    /**
     * 获取StockTransferIn对象
     *
     * @param stockTransferIn
     */
    private Stock getStock(StockTransferIn stockTransferIn){
        Stock stock = new Stock();
        stock.setListId(stockTransferIn.getListId());
        stock.setStockId(stockTransferIn.getStockId());
        stock.setStockName(stockTransferIn.getStockName());
        stock.setTypeId(stockTransferIn.getTypeId());
        stock.setTypeName(stockTransferIn.getTypeName());
        stock.setUnit(stockTransferIn.getUnit());
        stock.setBrand(stockTransferIn.getBrand());
        stock.setSpec(stockTransferIn.getSpec());
        stock.setAmount(stockTransferIn.getNum());

        return stock;
    }
}